<template>
	<div class="lm-chatgpt-item">
		<chatgpt-item-header text="正在为您生成结果：已有定时任务清单"></chatgpt-item-header>
		<div class="chatgpt-item-content">
			<div class="item-section-title">已有定时任务清单</div>
			<common-table ref="commonTable" :tableConfig="tableConfig" :hasPage="false" :border="false"></common-table>
		</div>
	</div>
</template>

<script>
import ChatgptItemHeader from './chatgpt-item-header'
import CommonTable from '@/components/common/common-table.vue'
import { mapState, mapMutations } from "vuex"
export default {
	name: 'OrdinaryTaskManage',
	props: {
		item: Object
	},
	components: {
		ChatgptItemHeader,
		CommonTable
	},
	data() {
		return {
			weekList: [],
			// 表格配置
			tableConfig: {
				total: 0,
				pageNo: 1,
				pageSize: 10,
				tData: [],
				tColumns: [
					{
						type: "index",
						width: 50
					},
					{
						title: '触发频率',
						key: 'task_type',
						width: 80,
						ellipsis: true,
						render: (h, params) => {
							return h('span', {}, this.getTaskTypeStr(params.row.task_type))
						}
					},
					{
						title: '触发时间',
						key: 'value1',
						width: 140,
						render: (h, params) => {
							return h('span', {}, this.getTimeStr(params.row.task_type, params.row.value1, params.row.value2))
						}
					},
					{
						title: '事件描述',
						key: 'task_describe',
						minWidth: 88,
						ellipsis: true,
					},
					{
						title: '操作',
						key: 'action',
						width: 88,
						className: 'table-opt',
						// fixed: 'right',
						align: 'center',
						render: (h, params) => {
							return h('div', [
								h(
									'span',
									{
										class: `ope-btn`,
										on: {
											click: () => {
												this.deleteTaskOne(params.index, params.row)
											}
										}
									},
									'删除'
								),
							])
						}
					}
				],
			},
		}
	},
	watch: {
		item: {
			handler(val) {
				this.tableConfig.tData = val.tData
			},
			immediate: true
		}
	},
	created() {
		this.initWeekList()
	},
	methods: {
		// 初始化周列表
		initWeekList() {
			for (let i = 0; i < 7; i++) {
				let list = ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
				this.weekList.push({
					label: list[i],
					value: i + 1
				})
			}
		},
		getTaskTypeStr(type) {
			let frequencyList = [
				{
					label: '单次',
					type: '0'
				},
				{
					label: '每日',
					type: '1'
				},
				{
					label: '每周',
					type: '2'
				},
				{
					label: '每月',
					type: '3'
				},
				{
					label: '每年',
					type: '4'
				}
			]
			return frequencyList.find(item => item.type == type).label
		},
		getTimeStr(type, value1, value2) {
			switch (type) {
				case '0':
					return this.$public.dateFormat(new Date(value1))
				case '1':
					return value1
				case '2':
					return this.weekList.find(item => item.value == value1).label
				case '3':
					return value1 + '日'
				case '4':
					return value1 + '月' + value2 +'日'
			}
		},
		// 删除
		deleteTaskOne(index, row) {
			this.$hMsgBox.confirm({
				title: '确定删除该定时任务吗？',
				content: "",
				onOk: () => {
					this.$usePost(this.$url.deleteTaskOne, {
						mark_id: row.mark_id
					}).then(res => {
						this.$refs.commonTable.handleDelData(index)
						this.$public.message(this, 'success', '删除成功')
					})
				}
			})
		}
	}
}
</script>

<style lang="scss" scoped>
.item-section-title{
	margin-bottom: 18px;
}
/deep/ .red{
	color: #E42222;
}
</style>